package com.example.aicloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.example.aicloud.common.ModelType;
import com.example.aicloud.common.Models;
import com.example.aicloud.entity.Answer;
import com.example.aicloud.service.IAnswerService;
import com.example.aicloud.utils.Result;
import com.example.aicloud.utils.SecurityUtil;
import io.netty.util.internal.StringUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.OpenAiImageClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Description: TODO
 * @Author Mr.Tang
 */
@RestController
@RequestMapping("/openai")
public class OpenAIController {

    @Resource
    private OpenAiChatClient chatModel;

    @Resource
    private OpenAiImageClient imageModel;

    @Resource
    private IAnswerService answerService;

    /**
     * 调用 Open AI 聊天接口
     * @param question 问题
     * @return
     */
    @RequestMapping("/chat")
    public Result chat(String question, HttpServletRequest request) {
        if (!StringUtils.hasLength(question)) {
            // 输入为空
            return Result.fail("请先输入内容!");
        }
        // 调用 Open AI 接口
        String ret = chatModel.call(question);
        // 将结果保存到数据库
        Answer answer = new Answer();
        answer.setTitle(question);
        answer.setContent(ret);
        answer.setModel(1);
        answer.setType(1);
        answer.setUid(SecurityUtil.getCurrentUser().getUid());
        boolean result = answerService.save(answer);
        if (!result){
            return Result.fail("服务器睡着了, 请重试");
        }

        return Result.success(ret);
    }

    /**
     * 实现绘图功能
     * @param question
     * @return
     */
    @RequestMapping("/draw")
    public Result draw(String question) {
        if (!StringUtils.hasLength(question)) {
            // 输入为空
            return Result.fail("请先输入内容!");
        }
        // 调用 Open AI 接口
        ImageResponse result = imageModel.call(new ImagePrompt(question));
        String imgUrl = result.getResult().getOutput().getUrl();

        // 将结果保存到数据库
        Answer answer = new Answer();
        answer.setTitle(question);
        answer.setContent(imgUrl);
        answer.setModel(1);
        answer.setType(2);
        answer.setUid(SecurityUtil.getCurrentUser().getUid());
        boolean save = answerService.save(answer);
        if (!save) {
            return Result.fail("服务器睡着了, 请重试");
        }
        return Result.success(imgUrl);
    }


    /**
     * 获取 OpenAI 聊天的历史信息
     * @param request
     * @return
     */
    @RequestMapping("/getchatlist")
    public Result getChatList() {
        long uid = SecurityUtil.getCurrentUser().getUid();
        // 查询历史信息
        QueryWrapper<Answer> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("type", ModelType.CHAT.getType());
        wrapper.eq("model", Models.OPEN_AI.getModel());
        wrapper.orderByDesc("aid");
        List<Answer> list = answerService.list(wrapper);
        // 返回
        return Result.success(list);
    }


    /**
     * 获取绘图历史信息
     * @return
     */
    @RequestMapping("/getdrawlist")
    public Result getDrawList() {
        long uid = SecurityUtil.getCurrentUser().getUid();
        // 查询历史信息
        QueryWrapper<Answer> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("type", ModelType.DRAW.getType());
        wrapper.eq("model", Models.OPEN_AI.getModel());
        wrapper.orderByDesc("aid");
        List<Answer> list = answerService.list(wrapper);
        // 返回
        return Result.success(list);
    }





}
